package com.yang.train.business.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 缘友一世
 * date 2025-01-02-16:07
 */
@Configuration
public class RedissonClientConfig {
    @Value("${spring.data.redis.host}")
    private String host;
    @Value("${spring.data.redis.port}")
    private String port;
    @Value("${spring.data.redis.password}")
    private String password;

    @Value("${spring.data.redis.timeout}")
    private int timeout;

    @Value("${spring.data.redis.connect-timeout}")
    private int connectTimeout;

    @Bean
    public RedissonClient getRedissonClient() {
        Config config = new Config();
        // 使用单节点模式配置Redis连接
        config.useSingleServer()
                .setAddress("redis://" + host + ":" + port)
                .setPassword(password)
                .setRetryInterval(3000)       // 自定义重试间隔 ：3 秒
                .setTimeout(timeout)          // 操作超时：5 秒
                .setConnectTimeout(connectTimeout); // 连接超时：2 秒
        return Redisson.create(config);
    }
}
